package br.com.loja.util.i18n;

import java.util.Locale;
import java.util.ResourceBundle;
import br.com.loja.util.PropertiesApp;
import java.util.MissingResourceException;

/**
 * @brief Classe MessagesBundle
 * @author David Luiz
 * @date 19/11/2014
 */
public class Language {

    private final Locale locale;
    private static Language instance;
    private ResourceBundle resourceBundle = null;

    private Language() {

        String propertyValue[];
        propertyValue = PropertiesApp.getInstance().getPropertyValue("locale").split("_");
        locale = new Locale(propertyValue[0], propertyValue[1]);
        try {
            resourceBundle = ResourceBundle.getBundle("br/com/loja/util/i18n/language/language", locale);
        } catch (MissingResourceException e) {
            resourceBundle = null;
        }

    }

    public static Language getInstance() {
        if (instance != null) {
            return instance;
        }

        synchronized (Language.class) {
            if (instance != null) {
                return instance;
            }

            instance = new Language();
            return instance;

        }

    }

    public String getString(String message) {

        if (message != null) {
            message = message.replace(" ", ".");
        }

        try {
            return resourceBundle.getString(message);
        } catch (Exception e) {
            //TODO: implementar log
            System.err.println(e.getMessage());
            e.printStackTrace();
            return message;
        }

    }

    public Locale getLocale() {
        return locale;
    }

}
